#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask_wtf import FlaskForm
# 字符串字段和密码字段,表单提交字段
from wtforms import StringField, PasswordField, SubmitField, FileField, \
    TextAreaField, SelectField, SelectMultipleField, BooleanField
# 调用验证器
from wtforms.validators import DataRequired, ValidationError, EqualTo
# 导入Admin 数据模型
from app.models import Admin, Tag, Auth, Role

tags = Tag.query.all()

auth_list = Auth.query.all()

role_list = Role.query.all()


class LoginForm(FlaskForm):
    """管理员登录表单"""
    account = StringField(
        label="账号",
        validators=[
            DataRequired("请输入账号")
        ],
        description="账号",  # 描述
        render_kw={  # 附加选项
            "class": "form-control",
            "placeholder": "请输入账号！",
            "required": "required"
        }
    )
    pwd = PasswordField(
        label="密码",
        validators=[
            DataRequired("请输入密码")
        ],
        description="密码",
        render_kw={
            "class": "form-control",
            "placeholder": "请输入密码！",
            "required": "required"
        }
    )
    submit = SubmitField(
        '登录',
        render_kw={
            "class": "btn btn-primary btn-block btn-flat"
        }
    )

    def validate_account(self, field):  # 自定义验证器
        account = field.data
        # 查询admin 中name 是否存在表单中提交的account
        admin = Admin.query.filter_by(name=account).count()
        if admin == 0:
            raise ValidationError("账号不存在")


class TagForm(FlaskForm):
    name = StringField(
        label="名称",
        validators=[
            DataRequired("请输入标签")
        ],
        description="标签",
        render_kw={
            "class": "form-control",
            "id": "input_name",
            "placeholder": "请输入标签名称！",
            "required": "required"
        }
    )

    submit = SubmitField(
        '提交',
        render_kw={
            "class": "btn btn-primary"
        }
    )


class MovieForm(FlaskForm):
    def __init__(self, *args, **kwargs):
        super(MovieForm, self).__init__(*args, **kwargs)
        self.tag_id.choices=[(v.id, v.name) for v in Tag.query.all()]
    title = StringField(
        label="片名",
        validators=[
            DataRequired("请输入片名")
        ],
        description="片名",
        render_kw={
            "class": "form-control",
            "id": "input_title",
            "placeholder": "请输入影片名称！",
            "required": "required"
        }
    )
    url = FileField(
        label="文件路径",
        validators=[
            DataRequired("请上传文件路径")
        ],
        description="文件路径",
        render_kw={
            "id": "input_url",
        }
    )
    info = TextAreaField(
        label="简介",
        validators=[
            DataRequired("请输入简介")
        ],
        description="简介",
        render_kw={
            "class": "form-control",
            "row": "10",
        }
    )
    logo = FileField(
        label="影片封面",
        validators=[
            DataRequired("请上传影片封面")
        ],
        description="影片封面",
        render_kw={
            "id": "input_logo",
        }
    )
    star = SelectField(
        label="星级",
        validators=[
            DataRequired("请选择星级")
        ],
        coerce=int,
        choices=[(1, "1星级"), (2, "2星级"), (3, "3星级"), (4, "4星级"), (5, "5星级")],
        description="星级",
        render_kw={
            "class": "form-control",
            "id": "input_star",
        }
    )
    tag_id = SelectField(
        label="标签",
        validators=[
            DataRequired("请选择标签")
        ],
        coerce=int,
        choices=[(v.id, v.name) for v in tags],
        description="标签",
        render_kw={
            "class": "form-control",
            "id": "input_tag_id",
        }
    )
    area = StringField(
        label="地区",
        validators=[
            DataRequired("请输入地区")
        ],
        description="地区",
        render_kw={
            "class": "form-control",
            "id": "input_area",
            "placeholder": "请输入地区！"
        }
    )
    length = StringField(
        label="片长",
        validators=[
            DataRequired("请输入片长")
        ],
        description="片长",
        render_kw={
            "class": "form-control",
            "id": "input_length",
            "placeholder": "请输入片长！"
        }
    )
    release_time = StringField(
        label="上映时间",
        validators=[
            DataRequired("请输入上映时间")
        ],
        description="上映时间",
        render_kw={
            "class": "form-control",
            "id": "input_release_time",
            "placeholder": "请输入上映时间！"
        }
    )
    submit = SubmitField(
        '提交',
        render_kw={
            "class": "btn btn-primary"
        }
    )


class PreviewForm(FlaskForm):
    title = StringField(
        label="预告标题",
        validators=[
            DataRequired("请输入预告标题")
        ],
        description="预告标题",
        render_kw={
            "class": "form-control",
            "id": "input_title",
            "placeholder": "请输入预告标题！",
            "required": "required"
        }
    )
    logo = FileField(
        label="封面图片",
        validators=[
            DataRequired("请上传封面图片")
        ],
        description="封面图片",
        render_kw={
            "id": "input_url",
        }
    )
    submit = SubmitField(
        '提交',
        render_kw={
            "class": "btn btn-primary"
        }
    )


class PwdForm(FlaskForm):
    old_pwd = PasswordField(
        label="旧密码",
        validators=[
            DataRequired("请输入旧密码")
        ],
        description="旧密码",
        render_kw={
            "class": "form-control",
            "placeholder": "请输入旧密码！",
            "required": "required",
            "id": "input_pwd"
        }
    )
    new_pwd = PasswordField(
        label="新密码",
        validators=[
            DataRequired("请输入新密码")
        ],
        description="新密码",
        render_kw={
            "class": "form-control",
            "placeholder": "请输入新密码！",
            "required": "required",
            "id": "input_pwd"
        }
    )
    submit = SubmitField(
        '提交',
        render_kw={
            "class": "btn btn-primary"
        }
    )

    def validate_old_pwd(self, field):
        from flask import session
        pwd = field.data
        name = session['admin']
        admin = Admin.query.filter_by(
            name=name
        ).first()
        if not admin.check_pwd(pwd):
            raise ValidationError("密码输入错误")


class AuthForm(FlaskForm):
    name = StringField(
        label="权限名称",
        validators=[
            DataRequired("请输入权限")
        ],
        description="权限名称",
        render_kw={
            "class": "form-control",
            "id": "input_name",
            "placeholder": "请输入权限名称！",
            "required": "required"
        }
    )
    url = StringField(
        label="权限地址",
        validators=[
            DataRequired("请输入权限地址")
        ],
        description="权限地址",
        render_kw={
            "class": "form-control",
            "id": "input_name",
            "placeholder": "请输入权限地址！",
            "required": "required"
        }
    )

    submit = SubmitField(
        '提交',
        render_kw={
            "class": "btn btn-primary"
        }
    )


class RoleForm(FlaskForm):
    def __init__(self, *args, **kwargs):
        super(RoleForm, self).__init__(*args, **kwargs)
        self.auths.choices=[(v.id, v.name) for v in Auth.query.all()]
    name = StringField(
        label="角色名称",
        validators=[
            DataRequired("请输入角色")
        ],
        description="角色名称",
        render_kw={
            "class": "form-control",
            "id": "input_name",
            "placeholder": "请输入角色名称！",
            "required": "required"
        }
    )
    # BooleanField  SelectMultipleField
    auths = SelectMultipleField(
        label="权限列表",
        validators=[
            DataRequired("请输入权限列表")
        ],
        coerce=int,
        choices=[(v.id, v.name) for v in auth_list],
        description="权限列表",
        render_kw={
            "class": "form-control",
            "id": "input_name",
            "placeholder": "请输入权限列表！",
            # "required": "required"
        }
    )

    submit = SubmitField(
        '提交',
        render_kw={
            "class": "btn btn-primary"
        }
    )


class AdminForm(FlaskForm):
    def __init__(self, *args, **kwargs):
        super(AdminForm, self).__init__(*args, **kwargs)
        self.role_id.choices=[(v.id, v.name) for v in Role.query.all()]
    name = StringField(
        label="管理员名称",
        validators=[
            DataRequired("请输入管理员名称")
        ],
        description="管理员名称",
        render_kw={
            "class": "form-control",
            "placeholder": "请输入管理员名称！",
            "required": "required"
        }
    )
    pwd = PasswordField(
        label="密码",
        validators=[
            DataRequired("请输入密码")
        ],
        description="密码",
        render_kw={
            "class": "form-control",
            "placeholder": "请输入密码！",
            "id": "input_pwd",
            "required": "required"
        }
    )
    re_pwd = PasswordField(
        label="确认密码",
        validators=[
            DataRequired("请输入确认密码"),
            EqualTo("pwd", "两次密码不一致")
        ],
        description="确认密码",
        render_kw={
            "class": "form-control",
            "placeholder": "请再次输入密码！",
            "id": "input_re_pwd",
            "required": "required"
        }
    )
    role_id = SelectField(
        label="所属角色",
        validators=[
            DataRequired("请选择管理员角色")
        ],
        description="选择管理员角色",
        coerce=int,
        choices=[(v.id, v.name) for v in role_list],
        render_kw={
            "class": "form-control",
            "id": "input_role_id",
        }
    )
    submit = SubmitField(
        '提交',
        render_kw={
            "class": "btn btn-primary"
        }
    )
